<?php

namespace System\Controllers;

if (!defined('BASEPATH')) die('Access Denied.');

use \System\Models\DBText as DBText;

class Signup extends \System\Core\Controller
{
    
    private $data = array();
    
    public function __construct()
    {
        parent::__construct();
        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->library('validation');
        $this->load->model('dbtext');
        $this->load->model('timezones');
        $this->load->model('users');        
    }
    
    public function indexAction()
    {
        // Present the rules a member has to accept before 
        // he can become a member to the site.
        if (!$this->input->isPost())
        {
            $this->data['rules'] = $this->dbtext->getText(DBText::RULES);
            $this->view->templateRender('outside', 'signup/rules', 'Rules', $this->data);
        }
        else
        {
            // Check if he accepted them or not.
            if ($this->input->post('accept') !== FALSE)
            {
                $this->fc->siteRedirect('signup/form');
            }
            elseif ($this->input->post('reject') !== FALSE)
            {
                $this->fc->siteRedirect('login');
            }
        }
    }

    public function formAction()
    {
        // Show the signup form.
        $this->data['timezones'] = $this->timezones->getTimezones();
        $this->view->templateRender('outside', 'signup/form', 'Signup', $this->data);
    }

    public function submitAction()
    {
        if ($this->input->isPost())
        {
            // Validate the fields.
            $this->validation->addRule('userName', 'user name', 'trim|std|minlen=3|maxlen=20');
            $this->validation->addRule('email', 'e-mail', 'trim|email|maxlen=100');
            $this->validation->addRule('confirmEmail', 'confirm e-mail', 'trim|equal=email');
            $this->validation->addRule('password', 'password', 'trim|minlen=6|maxlen=15');
            $this->validation->addRule('confirmPassword', 'confirm password', 'trim|equal=password');
            $this->validation->addRule('timezone', 'timezone', 'gte=-12|lte=14');
            if (!$this->validation->run())
            {
                $this->data['errors'] = $this->validation->getErrors();
            }

            // Do some additional checks.

            if ($this->users->getIdForName($this->input->post('userName')) != FALSE)
            {
                $this->data['errors'][] = 'That username is already taken.';
            }

            if ($this->users->getIdForEmail($this->input->post('email')) != FALSE)
            {
                $this->data['errors'][] = 'The email is already in use on this site.';
            }

            // An error occurred, render the signup page again.
            if (isset($this->data['errors']))
            {
                $this->formAction();
                exit;
            }

            // Everything seems to be correct so add the user to the db.
            $user = $this->users->addUser($this->input->posts());

            if ($user['founder'])
            {
                $this->curuser->logIn($this->input->post('userName'), $this->input->post('password'));
                $this->fc->message('Signup Successfull!', 'Welcome founder! You have been automatically logged in, click ' . anchor('index', 'here') . ' to continue to the home page.');
            }
            else
            {   
                $convert = array(
                        'SITE_NAME' => $this->config->getItem('site_name'),
                        'USER_NAME' => $this->input->post('userName'),
                        'USER_EMAIL' => $this->input->post('email'),
                        'IP_ADDRESS' => $this->input->ipLongAddr(),
                        'REG_LINK' => $this->config->getItem('base_url').'access/confirm/id/'.$user['id'].'/code/'.$user['salt'], 
                );

                $message = $this->dbtext->getText(DBText::EMAILCONF, $convert);
                if (@mail($this->input->post('email'), 'Welcome to '.$this->config->getItem('site_name'), $message, "From: {$this->config->getItem('site_email')}"))
                {
                    $this->fc->message('Signup Successfull!', 'Thank you for signing up! A message has been sent to the address you specified with further information how to validate your account.');
                }
                else
                {
                    $this->fc->errorMsg('An error occurred while trying to send the email.');
                }
            }
        }
        else
        {
            $this->formAction();
        }
    }
}

?>
